<?php
namespace Admin\Controller;
class ContractController extends AdminController{
	//合同控制器
  //贷款合同送行（银行外勤）
  public $express_company = [1=>'顺丰快递','圆通快递','中通快递','申通快递','韵达快递'];//快递公司

  public function Loan_send()
  {
    $info=new CheckController();
    $list=$info->getCheck_status(13,'!=-1');
    $this->assign('list',$list);
    $this->display();
  }

  //贷款合同接收（银行驻点）
  public function Loan_back()
  {
    $info=new CheckController();
    $list=$info->getCheck_status(14,'!=-1');
    $this->assign('list',$list);
    $this->display();
  }

 //银行合同送回（银行驻点）
 public function Bank_back()
 {
    $info=new CheckController();
    $list=$info->getCheck_status(15,'!=-1');
    $this->assign('express_company',$this->express_company);
    $this->assign('list',$list);
    $this->display();
  }

  //银行合同接收（银行外勤）
  public function Bank_receive()
  {
    $info=new CheckController();
    $list=$info->getCheck_status(16,'!=-1');
    $this->assign('list',$list);
    $this->display();
  }



  //合同送公证处公证（银行外勤）
   public function Notarization(){
    $info=new CheckController();
    $list=$info->getCheck_status(17,'!=-1');
    $this->assign('list',$list);
    $this->display();
  }

    //公证书收回（银行外勤）
  // public function Notarization_back(){
  //   $info=new CheckController();
  //   $list=$info->getCheck_status(19,10);
  //   $this->assign('list',$list);
  //   $this->display();
  // }
//归档资料送行
  // public function archive_send(){
  //   $info=new CheckController();
  //   $list=$info->getCheck_status(28,10);
  //   $this->assign('list',$list);
  //   $this->display();
  // }


  //归档资料确认
  public function archive_confirm()
  {
    $info=new CheckController();
    $list=$info->getCheck_status(28,'!=-1');
    $this->assign('list',$list);
    $this->display();
  }

  //归档资料
  public function archive_data()
  {
    $where = '1=1';
    // 搜索
    $where .= I('get.user_name',false,'trim') != '' ? ' and u.user_name like "%'.I('get.user_name',false,'trim').'%"' : '';
    $where .= I('get.identity_no',false,'trim') != '' ? ' and u.identity_no like "%'.I('get.identity_no',false,'trim').'%"' : '';
    $where .= I('get.department_name',false,'trim') != '' ? ' and u.department_name like "%'.I('get.department_name',false,'trim').'%"' : '';

    $where.=' and u.check_status>=30 and u.status=10 and c.status=5';

    $list=$this->getList('u.*,c.status as data_status,b.bank_short_name','user u',$where,'',true,'left join app_data_check c on u.id=c.user_id left join  app_bank b on u.loan_bank_id=b.id');

    foreach ($list as $key => $value)
    {
      $group=$this->getInfo('*','group','id='.$value['gpid']);
      $list[$key]['group_name']=$group['name'];
    }

    $this->assign('list',$list);
    $this->display();
  }

  public function archive_dataInfo()
  {
    if(IS_POST){

         $user_id=$_POST['user_id'];
         $ziliao=$this->getInfo('*','data_check','user_id='.$user_id.' and status=10');
         if($ziliao){
           $this->error('资料已审核完成',U('Contract/archive_data'),0);
         }
         $_POST['archive_data']=implode(',',$_POST['file']);
         $_POST['update_time']=time();
         $_POST['admin_id']=session('adminInfo.id');

         if($_POST['type_status']==0){
             $_POST['status']=10;
             $log=array('user_id'=>$user_id,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'opinion'=>'资料已全部归档','note'=>'资料已全部归档','add_time'=>time(),'status'=>6,'check_status'=>44);
             M('check_log')->add($log);
         }

         $result=M('data_check')->where('user_id='.$user_id)->save($_POST);
        if($result){
             redirect('/Admin/Contract/archive_data');
           }else{
             $this->error('审核失败',U('Contract/archive_data'),0);
           }
    }else{
        $id=I('get.user_id',0,'intval');
        $detail=new ApplyController();
        $detail->get_info($id);
        $dataInfo=$this->getInfo('*','data_check','user_id='.$id);
        $dataInfo['file']=explode(',',$dataInfo['archive_data']);
        $userFiles = $this->getList('*','user_file','user_id = '.$id);
        $this->assign('dataInfo',$dataInfo);
        $this->assign('userFiles',$userFiles);
        $this->display();
    }
  }

	//改变状态
	public function set_status()
  {
		$id=I('get.id',0,'intval');
		$status=I('get.status',0,'intval');

    $info=$this->getInfo('*','user','id='.$id.' and check_status='.$status);
    if($info){
      $this->error('该操作已不在此状态中');
      //$this->error('该操作已不在此状态中',$_SERVER['HTTP_REFERER'],0);
    }
    $data=array('check_admin_id'=>session('adminInfo.id'),'update_time'=>time(),'check_status'=>$status);
    $result=M('user')->where('id='.$id)->save($data);
    // echo M()->getLastsql();
    // exit;
   if($result){

          $note=$this->getStatus($status);
          $log=array('user_id'=>$id,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>$note,'note'=>$note,'add_time'=>time(),'check_status'=>$status);
          M('check_log')->add($log);
          $this->success('操作成功');
    }else{
        	$this->error('操作失败',0);
        }
	}


	public function getStatus($status)
  {
		 switch ($status) {
          	case '14':
          	   $note='贷款合同已送行';
          		break;
          	case '15':
          	   $note='贷款合同已接收';
          		break;
          	case '16':
          	   $note='银行合同已送回';
          		break;
          	case '17':
          	  $note='银行合同已接收';
          		break;

          	case '19':
          	  $note='合同送公证处已公证';
          		break;
            case '20':
          	  $note='公证书已收回';
          		break;
          	case '21':
          	  $note='公证书已确认';
              break;
            case '23':
              $note='快递确认(渠道内勤)';
              break;
            case '24':
              $note='银行抵押合同列表已提交';
              break;
            case '27':
              $note='公司归档资料已转交';
              break;
           // case '27':
           //    $note='快递确认(抵押岗)';
           //    break;
           case '28':
              $note='银行归档资料已递交';
              break;
          case '29':
              $note='归档资料已送行';
              break;
          case '30':
              $note='归档资料已确认';
              break;
          	default:
          	  $note='未知操作';
          		break;
          }
          return $note;
          exit;

	}


 public function batch()
 {
    $id=explode(',',$_POST['ids']);
    $status=$_POST['status'];
    foreach ($id as $v) {
      if($v!=0){
         $info=$this->getInfo('*','user','id='.$v.' and check_status='.$status);
         if($info){
          $this->ajaxReturn(array('info'=>'该操作已不在此状态中','status'=>0));
          //$this->error('该操作已不在此状态中');
        }
        $data=array('check_admin_id'=>session('adminInfo.id'),'update_time'=>time(),'check_status'=>$status);
        $result=M('user')->where('id='.$v)->save($data);

        if($result){

              $note=$this->getStatus($status);
              $log=array('user_id'=>$v,'admin_id'=>session('adminInfo.id'),'admin_name'=>session('adminInfo.name'),'type'=>session('adminInfo.group_name'),'status'=>6,'opinion'=>$note,'note'=>$note,'add_time'=>time(),'check_status'=>$status);
              M('check_log')->add($log);
        }else{
          $this->ajaxReturn(array('info'=>'操作失败','status'=>0));
             // $this->error('操作失败',0);
          }
       }

    }
     $this->ajaxReturn(array('info'=>'批量操作成功','status'=>1));
    // $this->success("批量操作成功");

 }


}